<?php
namespace app\admin\controller;
use app\admin\controller\Common;
use app\service\doorService;
use app\service\roomService;
use think\Db;
class Record extends Common{
    //记录管理
    public function recordList(){
         //查询小区信息
        $com = $this->pidTokenId();
        $whe["id"]=array('in',$com['community_id']);
        //$where['community_id'] = array('in',$com['community_id']);
        $communitylist = (new roomService())->communityList($whe);
        //Log::write($communitylist,"community");
        $community_id = $com['community_id'];
        $this->assign('communitylist',$communitylist);
        $data=request()->get();
        $where=array();
        if (!empty($data['community_id'])){
            $groups = (new roomService())->disgroup($data['community_id']);
            $this->assign('groups', $groups);
            // dump($data['group']);
            if (!empty($data['group'])) {
                $buildings = (new roomService())->disbuilding($data['community_id'],$data['group']);
                $this->assign('buildings', $buildings);
                // dump($buildings);exit();
                if (!empty($data['building'])) {
                    $units =  (new roomService())->disunit($data['community_id'], $data['group'],$data['building']);
                    $this->assign('units', $units);
                    if(!empty($data['unit'])){
                        $rooms = (new roomService())->disroom($data['community_id'], $data['group'],$data['building'],$data['unit']);
                        $this->assign('rooms',$rooms);
                    }
                }
            }
        }
        
        if(!empty($data)){
            if(!empty($data['community_id'])){
                $where['community_id'] = $data['community_id'];//小区名称
            }
            if(!empty($data['devicekey'])){
                $where['deviceKey'] = $data['devicekey'];//小区名称
            }
            if(!empty($data['group'])){
                $where['group'] = $data['group'];//组团
            }else{
                $where['group'] = "";
            }
            if(!empty($data['building'])){
                $where['building'] = $data['building'];
            }else{
                $where['building'] = "";
            }
            if(!empty($data['unit'])){
                $where['unit'] = $data['unit'];
            }else{
                $where['unit'] = "";
            }
            if(!empty($data['room'])){
                $where['room'] = $data['room'];
            }else{
                $where['room'] = "";
            }
            if(!empty($data['owner_name'])){
                $where['name'] = array("like","%".$data['owner_name']."%");
            }else{
                $where['name'] = "";
            }
            if(!empty($data['phone'])){
                $where['phone'] = array("like","%".$data['phone']."%");
            }else{
                $where['phone'] = "";
            }
        }
        $where = array_filter($where);
        $where['relation']=0;
        $devicelist=Db::name('device')->where("community_id","in",$com['community_id'])->select();
        $this->assign("devicelist",$devicelist);
        $page = request()->has('page') ? request()->get('page') : 1;
        $recordlist = (new doorService())->recordList($where,$page,$com['community_id']);
        //var_dump($recordlist);die;
        $number =Db::table("record")->where($where)->count();
        $this->assign('number',$number);
        $this->assign('list',$recordlist);
        $this->assign('recordlist',$recordlist);
        foreach($data as $k => $v) {
            $this->assign($k, $v);
        }
        return $this->fetch('record_list');
    } 

    //家人
    public function familyrecord(){
         //查询小区信息
        $com = $this->pidTokenId();
        $whe["id"]=array('in',$com['community_id']);
        //$where['community_id'] = array('in',$com['community_id']);
        $communitylist = (new roomService())->communityList($whe);
        //Log::write($communitylist,"community");
        $community_id = $com['community_id'];
        $this->assign('communitylist',$communitylist);
        $data=request()->get();
        $where=array();
        if (!empty($data['community_id'])){
            $groups = (new roomService())->disgroup($data['community_id']);
            $this->assign('groups', $groups);
            // dump($data['group']);
            if (!empty($data['group'])) {
                $buildings = (new roomService())->disbuilding($data['community_id'],$data['group']);
                $this->assign('buildings', $buildings);
                // dump($buildings);exit();
                if (!empty($data['building'])) {
                    $units =  (new roomService())->disunit($data['community_id'], $data['group'],$data['building']);
                    $this->assign('units', $units);
                    if(!empty($data['unit'])){
                        $rooms = (new roomService())->disroom($data['community_id'], $data['group'],$data['building'],$data['unit']);
                        $this->assign('rooms',$rooms);
                    }
                }
            }
        }
        
        if(!empty($data)){
            if(!empty($data['community_id'])){
                $where['community_id'] = $data['community_id'];//小区名称
            }else{
                $where['community_id'] = "";
            }
            if(!empty($data['devicekey'])){
                $where['deviceKey'] = $data['devicekey'];//小区名称
            }
            if(!empty($data['group'])){
                $where['group'] = $data['group'];//组团
            }else{
                $where['group'] = "";
            }
            if(!empty($data['building'])){
                $where['building'] = $data['building'];
            }else{
                $where['building'] = "";
            }
            if(!empty($data['unit'])){
                $where['unit'] = $data['unit'];
            }else{
                $where['unit'] = "";
            }
            if(!empty($data['room'])){
                $where['room'] = $data['room'];
            }else{
                $where['room'] = "";
            }
            if(!empty($data['owner_name'])){
                $where['name'] = array("like","%".$data['owner_name']."%");
            }else{
                $where['name'] = "";
            }
            if(!empty($data['phone'])){
                $where['phone'] = array("like","%".$data['phone']."%");
            }else{
                $where['phone'] = "";
            }
        }
        $where = array_filter($where);
        $where['relation']=1;
        $devicelist=Db::name('device')->where("community_id","in",$com['community_id'])->select();
        $this->assign("devicelist",$devicelist);
        $page = request()->has('page') ? request()->get('page') : 1;
        $recordlist = (new doorService())->recordList($where,$page,$com['community_id']);
        $number =Db::table("record")->where($where)->count();
        $this->assign('number',$number);
        $this->assign('list',$recordlist);
        $this->assign('recordlist',$recordlist);
        foreach($data as $k => $v) {
            $this->assign($k, $v);
        }
        return $this->fetch('family_record');
    }

    //租户列表
    public function tenantrecord(){
         //查询小区信息
        $com = $this->pidTokenId();
        $whe["id"]=array('in',$com['community_id']);
        //$where['community_id'] = array('in',$com['community_id']);
        $communitylist = (new roomService())->communityList($whe);
        //Log::write($communitylist,"community");
        $community_id = $com['community_id'];
        $this->assign('communitylist',$communitylist);
        $data=request()->get();
        $where=array();
        if (!empty($data['community_id'])){
            $groups = (new roomService())->disgroup($data['community_id']);
            $this->assign('groups', $groups);
            // dump($data['group']);
            if (!empty($data['group'])) {
                $buildings = (new roomService())->disbuilding($data['community_id'],$data['group']);
                $this->assign('buildings', $buildings);
                // dump($buildings);exit();
                if (!empty($data['building'])) {
                    $units =  (new roomService())->disunit($data['community_id'], $data['group'],$data['building']);
                    $this->assign('units', $units);
                    if(!empty($data['unit'])){
                        $rooms = (new roomService())->disroom($data['community_id'], $data['group'],$data['building'],$data['unit']);
                        $this->assign('rooms',$rooms);
                    }
                }
            }
        }
        
        if(!empty($data)){
            if(!empty($data['community_id'])){
                $where['community_id'] = $data['community_id'];//小区名称
            }else{
                $where['community_id'] = "";
            }
            if(!empty($data['devicekey'])){
                $where['deviceKey'] = $data['devicekey'];//小区名称
            }
            if(!empty($data['group'])){
                $where['group'] = $data['group'];//组团
            }else{
                $where['group'] = "";
            }
            if(!empty($data['building'])){
                $where['building'] = $data['building'];
            }else{
                $where['building'] = "";
            }
            if(!empty($data['unit'])){
                $where['unit'] = $data['unit'];
            }else{
                $where['unit'] = "";
            }
            if(!empty($data['room'])){
                $where['room'] = $data['room'];
            }else{
                $where['room'] = "";
            }
            if(!empty($data['owner_name'])){
                $where['name'] = array("like","%".$data['owner_name']."%");
            }else{
                $where['name'] = "";
            }
            if(!empty($data['phone'])){
                $where['phone'] = array("like","%".$data['phone']."%");
            }else{
                $where['phone'] = "";
            }
        }
        $where = array_filter($where);
        $where['relation']=2;
        $devicelist=Db::name('device')->where("community_id","in",$com['community_id'])->select();
        $this->assign("devicelist",$devicelist);
        $page = request()->has('page') ? request()->get('page') : 1;
        $recordlist = (new doorService())->recordList($where,$page,$com['community_id']);
        $number =Db::table("record")->where($where)->count();
        $this->assign('number',$number);
        $this->assign('list',$recordlist);
        $this->assign('recordlist',$recordlist);
        foreach($data as $k => $v) {
            $this->assign($k, $v);
        }
        
        return $this->fetch("tenant_record");
    }

    public function visitorFaceRecord(){
         //查询小区信息
        $com = $this->pidTokenId();
        $whe["id"]=array('in',$com['community_id']);
        //$where['community_id'] = array('in',$com['community_id']);
        $communitylist = (new roomService())->communityList($whe);
        //Log::write($communitylist,"community");
        $community_id = $com['community_id'];
        $this->assign('communitylist',$communitylist);
        $data=request()->get();
        $where=array();
        if (!empty($data['community_id'])){
            $groups = (new roomService())->disgroup($data['community_id']);
            $this->assign('groups', $groups);
            // dump($data['group']);
            if (!empty($data['group'])) {
                $buildings = (new roomService())->disbuilding($data['community_id'],$data['group']);
                $this->assign('buildings', $buildings);
                // dump($buildings);exit();
                if (!empty($data['building'])) {
                    $units =  (new roomService())->disunit($data['community_id'], $data['group'],$data['building']);
                    $this->assign('units', $units);
                    if(!empty($data['unit'])){
                        $rooms = (new roomService())->disroom($data['community_id'], $data['group'],$data['building'],$data['unit']);
                        $this->assign('rooms',$rooms);
                    }
                }
            }
        }
        
        if(!empty($data)){
            if(!empty($data['community_id'])){
                $where['community_id'] = $data['community_id'];//小区名称
            }else{
                $where['community_id'] = "";
            }
            if(!empty($data['devicekey'])){
                $where['deviceKey'] = $data['devicekey'];//小区名称
            }
            if(!empty($data['group'])){
                $where['group'] = $data['group'];//组团
            }else{
                $where['group'] = "";
            }
            if(!empty($data['building'])){
                $where['building'] = $data['building'];
            }else{
                $where['building'] = "";
            }
            if(!empty($data['unit'])){
                $where['unit'] = $data['unit'];
            }else{
                $where['unit'] = "";
            }
            if(!empty($data['room'])){
                $where['room'] = $data['room'];
            }else{
                $where['room'] = "";
            }
            if(!empty($data['owner_name'])){
                $where['name'] = array("like","%".$data['owner_name']."%");
            }else{
                $where['name'] = "";
            }
            if(!empty($data['phone'])){
                $where['phone'] = array("like","%".$data['phone']."%");
            }else{
                $where['phone'] = "";
            }
        }
        $where = array_filter($where);
        $where['relation']=3;
        $where["recMode"]=1;
        $devicelist=Db::name('device')->where("community_id","in",$com['community_id'])->select();
        $this->assign("devicelist",$devicelist);
        $page = request()->has('page') ? request()->get('page') : 1;
        $recordlist = (new doorService())->recordList($where,$page,$com['community_id']);
        $number =Db::table("record")->where($where)->count();
        $this->assign('number',$number);
        $this->assign('list',$recordlist);
        $this->assign('recordlist',$recordlist);
        foreach($data as $k => $v) {
            $this->assign($k, $v);
        }
        return $this->fetch('visitor_face_record');
    }

    
    public function visitorIdcardRecord(){
         //查询小区信息
        $com = $this->pidTokenId();
        $whe["id"]=array('in',$com['community_id']);
        //$where['community_id'] = array('in',$com['community_id']);
        $communitylist = (new roomService())->communityList($whe);
        //Log::write($communitylist,"community");
        $community_id = $com['community_id'];
        $this->assign('communitylist',$communitylist);
        $data=request()->get();
        $where=array();
        if (!empty($data['community_id'])){
            $groups = (new roomService())->disgroup($data['community_id']);
            $this->assign('groups', $groups);
            // dump($data['group']);
            if (!empty($data['group'])) {
                $buildings = (new roomService())->disbuilding($data['community_id'],$data['group']);
                $this->assign('buildings', $buildings);
                // dump($buildings);exit();
                if (!empty($data['building'])) {
                    $units =  (new roomService())->disunit($data['community_id'], $data['group'],$data['building']);
                    $this->assign('units', $units);
                    if(!empty($data['unit'])){
                        $rooms = (new roomService())->disroom($data['community_id'], $data['group'],$data['building'],$data['unit']);
                        $this->assign('rooms',$rooms);
                    }
                }
            }
        }
        
        if(!empty($data)){
            if(!empty($data['community_id'])){
                $where['community_id'] = $data['community_id'];//小区名称
            }else{
                $where['community_id'] = "";
            }
            if(!empty($data['devicekey'])){
                $where['deviceKey'] = $data['devicekey'];//小区名称
            }
            if(!empty($data['group'])){
                $where['group'] = $data['group'];//组团
            }else{
                $where['group'] = "";
            }
            if(!empty($data['building'])){
                $where['building'] = $data['building'];
            }else{
                $where['building'] = "";
            }
            if(!empty($data['unit'])){
                $where['unit'] = $data['unit'];
            }else{
                $where['unit'] = "";
            }
            if(!empty($data['room'])){
                $where['room'] = $data['room'];
            }else{
                $where['room'] = "";
            }
            if(!empty($data['owner_name'])){
                $where['name'] = array("like","%".$data['owner_name']."%");
            }else{
                $where['name'] = "";
            }
            if(!empty($data['phone'])){
                $where['phone'] = array("like","%".$data['phone']."%");
            }else{
                $where['phone'] = "";
            }
        }
        $where = array_filter($where);
        $where['relation']=3;
        $where["recMode"]=4;
        $devicelist=Db::name('device')->where("community_id","in",$com['community_id'])->select();
        $this->assign("devicelist",$devicelist);
        $page = request()->has('page') ? request()->get('page') : 1;
        $recordlist = (new doorService())->recordList($where,$page,$com['community_id']);
        $number =Db::table("record")->where($where)->count();
        $this->assign('number',$number);
        $this->assign('list',$recordlist);
        $this->assign('recordlist',$recordlist);
        foreach($data as $k => $v) {
            $this->assign($k, $v);
        }
        return $this->fetch("visitor_idcard_record");
    }
    
    //租户列表
    public function strangerrecord(){
         //查询小区信息
        $com = $this->pidTokenId();
        $whe["id"]=array('in',$com['community_id']);
        //$where['community_id'] = array('in',$com['community_id']);
        $communitylist = (new roomService())->communityList($whe);
        //Log::write($communitylist,"community");
        $community_id = $com['community_id'];
        $this->assign('communitylist',$communitylist);
        $data=request()->get();
        $where=array();
        if (!empty($data['community_id'])){
            $groups = (new roomService())->disgroup($data['community_id']);
            $this->assign('groups', $groups);
            // dump($data['group']);
            if (!empty($data['group'])) {
                $buildings = (new roomService())->disbuilding($data['community_id'],$data['group']);
                $this->assign('buildings', $buildings);
                // dump($buildings);exit();
                if (!empty($data['building'])) {
                    $units =  (new roomService())->disunit($data['community_id'], $data['group'],$data['building']);
                    $this->assign('units', $units);
                    if(!empty($data['unit'])){
                        $rooms = (new roomService())->disroom($data['community_id'], $data['group'],$data['building'],$data['unit']);
                        $this->assign('rooms',$rooms);
                    }
                }
            }
        }
        
        if(!empty($data)){
            if(!empty($data['community_id'])){
                $where['community_id'] = $data['community_id'];//小区名称
            }else{
                $where['community_id'] = "";
            }
            if(!empty($data['devicekey'])){
                $where['deviceKey'] = $data['devicekey'];//小区名称
            }
            if(!empty($data['group'])){
                $where['group'] = $data['group'];//组团
            }else{
                $where['group'] = "";
            }
            if(!empty($data['building'])){
                $where['building'] = $data['building'];
            }else{
                $where['building'] = "";
            }
            if(!empty($data['unit'])){
                $where['unit'] = $data['unit'];
            }else{
                $where['unit'] = "";
            }
            if(!empty($data['room'])){
                $where['room'] = $data['room'];
            }else{
                $where['room'] = "";
            }
            if(!empty($data['owner_name'])){
                $where['name'] = array("like","%".$data['owner_name']."%");
            }else{
                $where['name'] = "";
            }
            if(!empty($data['phone'])){
                $where['phone'] = array("like","%".$data['phone']."%");
            }else{
                $where['phone'] = "";
            }
        }
        $where = array_filter($where);
        $where['relation']=6;
        $devicelist=Db::name('device')->where("community_id","in",$com['community_id'])->select();
        $this->assign("devicelist",$devicelist);
        $page = request()->has('page') ? request()->get('page') : 1;
        $recordlist = (new doorService())->recordList($where,$page,$com['community_id']);
        $number =Db::table("record")->where($where)->count();
        $this->assign('number',$number);
        $this->assign('list',$recordlist);
        $this->assign('recordlist',$recordlist);
        foreach($data as $k => $v) {
            $this->assign($k, $v);
        }
        
        return $this->fetch("stranger_record");
    }
     public function object_to_array($obj){
        $_arr = is_object($obj) ? get_object_vars($obj) :$obj;
        foreach($_arr as $key=>$val){
            if($key !== 'gioouts'){
                $val = (is_array($val) || is_object($val)) ? $this->object_to_array($val):$val;
                $arr[$key] = $val;
            }
        }
        return $arr;
    }
    public function test(){
        $path=dir(getcwd())->path.'\callback\20200105';
        $address = $path."\\173158821554.txt";
        $path=dir(getcwd())->path.'\callback\20200108';
        $address = $path."\\155024338485.txt";
        
        //file_put_contents($address,file_get_contents("php://input"),FILE_APPEND);//将数据读入文本

        parse_str(file_get_contents($address),$result);//
        $re=array_key_exists("photoUrl", $result);
        echo "<pre>";
        var_dump($result);
    }
}